import config from 'config/env';
export const myRequest = (options) => { //暴露一个function：myRequest，使用options接收页面传过来的参数
	return new Promise((resolve, reject) => { //异步封装接口，使用Promise处理异步请求
		// if (uni.getStorageSync('user_info')) {
		// 	let userInfo = uni.getStorageSync('user_info');
		// 	if (userInfo.id == '1538797328970452993' && options.method == 'post' || options.method ==
		// 		'put' || options.method == 'delete') {
		// 		return uni.showModal({
		// 			title: '提示',
		// 			content: "演示环境，不可操作",
		// 			success(res) {}
		// 		});
		// 		reject();
		// 	}
		// }
		console.log("入参==" + JSON.stringify(options.data))
		if (options.showLoading) {
			uni.showLoading({
				title: '加载中'
			});
		}
		let url = options.url;
		//#ifndef H5 
		url = config.basePath + options.url
		//#endif
		console.log("url==" + JSON.stringify(url))
		uni.request({ //发送请求
			url: url, //接收请求的API
			method: options.method || 'GET', //接收请求的方式,如果不传默认为GET
			header: {
				'satoken': uni.getStorageSync('access_token'), // 请求token
			},
			data: options.data || {}, //接收请求的data,不传默认为空
			//接口请求成功
			success: (res) => {
				console.log(res)
				if (res.statusCode != 200) {
					return uni.showModal({
						title: '提示',
						content: res.data.msg || "未知错误",
						success(res) {}
					});
					reject();
				}
				if (res.data.code != 0) {
					if (res.data.code == 401) {
						uni.reLaunch({
							url: '/pages/login/index'
						})
						uni.removeStorageSync('access_token');
						uni.removeStorageSync('expires_in');
						reject("token过期");
					} else {
						return uni.showModal({
							title: '提示',
							content: res.data.msg || "未知错误",
							success(res) {}
						});
						reject();
					}
					reject();
				}
				resolve(res.data) //成功,将数据返回
			},
			fail: (err) => { //失败操作
				console.log("fail")
				uni.showModal({
					title: '提示',
					content: '接口请求出错：' + err.errMsg,
					success(res) {}
				});
				reject(err)
			},
			complete(res) {
				if (options.showLoading) {
					uni.hideLoading();
				}
			}
		})
	})
}
